package com.yonyou.pmclouds.basecom.util;

import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;

/**
 * 经纬度计算距离工具类
 *
 * @author th
 * @date 2019/9/3 16:27
 */
public final class DistanceUtils {

    /**
     * 根据经纬度计算两点之间的距离（米）
     *
     * @param longitude1
     * @param latitude1
     * @param longitude2
     * @param latitude2
     * @return
     */
    public static double getDistanceMeter(double longitude1, double latitude1, double longitude2, double latitude2) {
        GlobalCoordinates gpsFrom = new GlobalCoordinates(latitude1, longitude1);
        GlobalCoordinates gpsTo = new GlobalCoordinates(latitude2, longitude2);
        //创建GeodeticCalculator，调用计算方法，传入坐标系、经纬度用于计算距离
        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(Ellipsoid.Sphere, gpsFrom, gpsTo);

        return geoCurve.getEllipsoidalDistance();
    }

}
